package
{
	import com.flashartofwar.fcss.styles.IStyle;
	import com.flashartofwar.fcss.stylesheets.StyleSheet;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.getTimer;

	public class FCSSSpeedTest extends Sprite
	{
		private var rawCSS : String;
		private var urlLoder : URLLoader;
		private var styleSheet:StyleSheet;

		public function FCSSSpeedTest()
		{
			urlLoder = new URLLoader( );
			urlLoder.addEventListener( Event.COMPLETE, onCSSLoad);
			urlLoder.load(new URLRequest("http://www.boostup.org/pub/n/css/base.styles.css"));

		}

		protected function parseCamoCSS():void
		{
			styleSheet = new StyleSheet();	
			trace("Test FCSS Pre-parser");
			// parse css	
			var t:Number = getTimer();
			styleSheet.parseCSS(rawCSS, true);
			t = (getTimer()-t);
			var strDebug:String;
			strDebug = "parsing time: " + t + " ms\n";
			strDebug += "bytes: " + rawCSS.length + " (" + Math.round(rawCSS.length / t) + " bytes/ms)\n--\n";
			trace(strDebug);
		}

		private function onCSSLoad(event : Event) : void
		{
			rawCSS = URLLoader(event.target ).data;
			parseCamoCSS();
			requestAllSelectors();
		}

		protected function requestAllSelectors():void
		{
			trace("Test Requesting Every Selector");
			var t:Number = getTimer();
			var strDebug:String;

			var selectors:Array = styleSheet.selectorNames;
			var total:int = selectors.length;
			var i:int;
			var style:IStyle;
			var styleStartTime:Number;
			var styleName:String;

			for(i = 0; i < total; i++)
			{
				styleName = selectors[i];
				trace("Getting '"+styleName+"' Selector #",i,"of",(total-1));
				styleStartTime = getTimer();
				style = styleSheet.getSelector(styleName);
				styleStartTime = (getTimer()-styleStartTime);
				strDebug = "parsing time: " + styleStartTime + " ms\n";
				trace(strDebug);
			}
			t = (getTimer()-t);

			strDebug = "total time to request "+(total-1)+" selectors time: " + t + " ms\n";
			trace(strDebug);
		}
	}
}

